package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.text.InputType
import android.view.View
import com.baizan.base.BaseActivity
import com.baizan.daka.Constants
import com.baizan.daka.ui.contract.LoginContract
import com.baizan.daka.mvp.model.bean.LoginInfoBean
import com.baizan.daka.ui.presenter.LoginPresenter
import com.baizan.daka.showToast
import com.baizan.daka.startActivity
import com.baizan.daka.ui.contract.ModifyPasswordContract
import com.baizan.daka.ui.presenter.ModifyPasswordPresenter
import com.baizan.daka.util.Preference
import com.baizan.daka.util.StringMatches
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_modify_password.*

class ModifyPasswordActivity : BaseActivity<ModifyPasswordPresenter>(), ModifyPasswordContract.View {

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, ModifyPasswordActivity::class.java)
            context.startActivity(intent)

        }
    }
    override fun initPresenter(): ModifyPasswordPresenter {
        return ModifyPasswordPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_modify_password
    }

    override fun initData() {
       val userInfoBean =Constants.userInfoBean
        userInfoBean?. let {
            account_et.setText(userInfoBean.account)
            account_et.isEnabled =false
        } ?:let{
            showToast("用户信息异常")
            finish()
            return
        }
        password_et.isPressed = true
        new_password_et.isPressed = true
        new_password2_et.isPressed = true
        eye_iv.isSelected = true
        new_eye_iv.isSelected = true
        new_eye_2_iv.isSelected = true
        submit_bt.setOnClickListener {
            var account = account_et.text
            if (account.isEmpty()) {
                showToast("请输入用户名/邮箱")
                return@setOnClickListener
            }
            var password = password_et.text
            if (password.isEmpty()) {
                showToast("请输入旧密码")
                return@setOnClickListener
            }
            var newPassword1 = new_password_et.text
            if (newPassword1.isEmpty()) {
                showToast("请输入新密码")
                return@setOnClickListener
            }
            var newPassword2 = new_password2_et.text
            if (newPassword2.isEmpty()) {
                showToast("请输入再次输入新密码")
                return@setOnClickListener
            }
            if(!newPassword1.toString().equals(newPassword2.toString()) ){
                showToast("俩次输入新密码不一致")
                return@setOnClickListener
            }
            if(!StringMatches.matchesPassword(newPassword1.toString())){
                showToast("请输入8-16位数字加字母的密码")
                return@setOnClickListener
            }
            presenter?.modifyPassword(account.toString(), password.toString(),newPassword1.toString())
        }
        eye_iv.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected)
                password_et.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            else
                password_et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

        }
        new_eye_iv.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected)
                new_password_et.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            else
                new_password_et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

        }
        new_eye_2_iv.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected)
                new_password2_et.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            else
                new_password2_et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

        }
    }

    override fun initView() {

    }

    override fun start() {
    }


    override fun modifyPasswordSuccess() {
        showToast("修改密码成功")
//        startActivity<HomeActivity>()
        finish()
    }


}